package com.xiongda.service.impl;

import com.xiongda.common.ServerResponse;
import com.xiongda.dao.SafetyProblemMapper;
import com.xiongda.dao.UserMapper;
import com.xiongda.dto.UserDto;
import com.xiongda.pojo.SafetyProblem;
import com.xiongda.pojo.User;
import com.xiongda.service.UserService;
import com.xiongda.tools.MD5Util;
import com.xiongda.tools.MailUitls;
import com.xiongda.tools.PatternUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * 用户服务层实现
 *
 * @author 缪琼
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SafetyProblemMapper safetyProblemMapper;

    @Override
    public ServerResponse<User> login(UserDto userDto) {
        if ("".equals(userDto.getUserName()))
            return ServerResponse.createByErrorMessage("用户名不能为空");
        if ("".equals(userDto.getPassword()))
            return ServerResponse.createByErrorMessage("密码不能为空");
        String passwordMD5 = MD5Util.MD5EncodeUtf8(userDto.getPassword());
        User user = new User();
        user.setUserName(userDto.getUserName());
        user.setPassword(passwordMD5);
        user = userMapper.getUser(user);
        if (user == null)
            return ServerResponse.createByErrorMessage("用户名或密码错误");
        if (user.getActivated() == 0)
            return ServerResponse.createByErrorMessage("用户未激活");
        if (user.getLocked() == 0)
            return ServerResponse.createByErrorMessage("用户已被冻结");
        userMapper.updateByLastLoginTime(user.getId());
        user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);
        return ServerResponse.createBySuccess("登陆成功", user);
    }

    @Override
    @Transactional
    public ServerResponse<User> register(UserDto userDto) {
        if (org.apache.commons.lang3.StringUtils.isEmpty(userDto.getUserName()))
            return ServerResponse.createByErrorMessage("用户名不能为空");
        if (org.apache.commons.lang3.StringUtils.isEmpty(userDto.getPassword()))
            return ServerResponse.createByErrorMessage("密码不能为空");
        if (!PatternUtil.isEmail(userDto.getEmail()))
            return ServerResponse.createByErrorMessage("邮箱格式错误");
        User user = new User();
        user.setUserName(userDto.getUserName());
        user = userMapper.getUser(user);
        if (user != null)
            return ServerResponse.createByErrorMessage("用户名已存在");
        user = new User();
        user.setEmail(userDto.getEmail());
        user = userMapper.getUser(user);
        if (user != null)
            return ServerResponse.createByErrorMessage("邮箱已被注册");
        user = new User();
        user.setUserName(userDto.getUserName());
        user.setPassword(MD5Util.MD5EncodeUtf8(userDto.getPassword()));
        user.setEmail(userDto.getEmail());
        user.setActivatedCode(UUID.randomUUID().toString());
        int count = userMapper.insert(user);
        if (count == 1) {
            String code = user.getActivatedCode();
            MailUitls.sendMail(userDto.getEmail(), code);
            return ServerResponse.createBySuccessMessage("注册成功");
        }
        return ServerResponse.createByErrorMessage("注册失败");
    }

    @Override
    public ServerResponse updateUserByCode(String code) {
        User user = userMapper.getUserByCode(code);
        if (user == null)
            return ServerResponse.createByErrorMessage("参数错误，无法完成激活");
        else if (user.getActivated() == 1)
            return ServerResponse.createByErrorMessage("用户已经激活过");
        else {
            user.setActivated(1);
            userMapper.updateByPrimaryKeySelective(user);
        }
        return ServerResponse.createBySuccessMessage("激活成功");
    }

    @Override
    public ServerResponse checkUserName(UserDto userDto) {
        if ("".equals(userDto.getUserName()))
            return ServerResponse.createByErrorMessage("用户名不能为空");
        User user = new User();
        user.setUserName(userDto.getUserName());
        User user1 = userMapper.getUser(user);
        if (user1 == null)
            return ServerResponse.createByErrorMessage("用户名不存在");
        if (user1.getActivated() == 0)
            return ServerResponse.createByErrorMessage("用户未激活过");
        if (user1.getLocked() == 0)
            return ServerResponse.createByErrorMessage("用户已被冻结");
        SafetyProblem safetyProblem = safetyProblemMapper.get(user1.getId());
        return ServerResponse.createBySuccess(safetyProblem);
    }

    @Override
    public ServerResponse getUserByUserName(String userName) {
        User user = new User();
        user.setUserName(userName);
        user = userMapper.getUser(user);
        if (user == null)
            return ServerResponse.createByErrorMessage("用户不存在");
        if (user.getActivated() == 0)
            return ServerResponse.createByErrorMessage("用户未激活");
        if (user.getLocked() == 0)
            return ServerResponse.createByErrorMessage("用户已被冻结");
        return ServerResponse.createBySuccess(user);
    }

    @Override
    public ServerResponse editPassword(UserDto userDto) {
        User user = new User();
        user.setId(userDto.getId());
        user.setPassword(MD5Util.MD5EncodeUtf8(userDto.getPassword()));
        int count = userMapper.updateByPrimaryKeySelective(user);
        if (count != 1)
            return ServerResponse.createByErrorMessage("修改失败");
        return ServerResponse.createBySuccessMessage("修改成功");
    }

    @Override
    public User getUser(Long id) {
        User user = new User();
        user.setId(id);
        return userMapper.getUser(user);
    }
}
